package models

import (
	"github.com/xormplus/xorm"
)

type DB interface {
	NewDAO() (DAO, error)
	Close()
}

type db struct {
	*xorm.Engine
}

func NewDB(driverName, dataSourceName string) (DB, error) {
	d, err := xorm.NewEngine(driverName, dataSourceName)
	if err != nil {
		return nil, err
	}
	if err = d.Ping(); err != nil {
		return nil, err
	}
	d.ShowSQL(true)
	d.ShowExecTime(true)
	d.SetLogLevel(0)
	d.Sync2(new(UserInfo), new(User))
	//d.Sync2()
	return &db{d}, nil
}

func (db *db) NewDAO() (DAO, error) {
	return &dBSession{db.Engine.NewSession()}, nil
}

func (db *db) Close() {
	db.Engine.Close()
}
